# 编译器和编译标志
CXX = g++
AR = ar

# 主文件及文件
MAIN_PATH := ./build
MAIN_FILE := main

# 各个路径
INCLUDE_PATH := ./include
SRC_PATH := ./src
OBJ_PATH := ./obj
LIB_PATH := ./lib
EXTERNAL_INCLUDE_PATH := /ucrt64/include
EXTERNAL_LIB_PATH := /ucrt64/lib
EXTERNAL_LIB := openblas

# 编译标志
CXXFLAGS = -fdiagnostics-color=always -fopenmp -Werror -O3 -march=native -I$(INCLUDE_PATH)
LINKFLAGS = -L$(LIB_PATH) -lxibo -fopenmp
EXTERNALFLAGS = -I$(EXTERNAL_INCLUDE_PATH) -L$(EXTERNAL_LIB_PATH) -l$(EXTERNAL_LIB)

# 源文件
INCLUDE_FILES := $(wildcard $(INCLUDE_PATH)/*.hpp)
SRC_FILES := $(wildcard $(SRC_PATH)/*.cpp)
LIB_FILES := $(wildcard $(LIB_PATH)/*.a)
OBJ_FILES := $(patsubst $(SRC_PATH)/%.cpp,$(OBJ_PATH)/%.o,$(SRC_FILES))

# 编译源文件
$(OBJ_PATH)/%.o: $(SRC_PATH)/%.cpp
	@mkdir -p $(OBJ_PATH)
	@$(CXX) -c $^ -o $@ $(CXXFLAGS) $(EXTERNALFLAGS)

# 建立静态库
$(LIB_PATH)/libxibo.a: $(OBJ_FILES)
	@mkdir -p $(LIB_PATH)
	@$(AR) -r $@ $^

lib_a : $(LIB_PATH)/libxibo.a

# 编译主文件
$(OBJ_PATH)/$(MAIN_FILE).o : $(MAIN_PATH)/$(MAIN_FILE).cpp
	@mkdir -p $(OBJ_PATH)
	@$(CXX) -c $^ -o $@ $(CXXFLAGS) $(EXTERNALFLAGS)

# 链接静态库
$(MAIN_PATH)/$(MAIN_FILE) : $(OBJ_PATH)/$(MAIN_FILE).o $(LIB_PATH)/libxibo.a
	@$(CXX) $^ -o $@ $(LINKFLAGS) $(EXTERNALFLAGS)

link_a : $(MAIN_PATH)/$(MAIN_FILE)

# 运行
run :
	$(MAIN_PATH)/$(MAIN_FILE)

# 编译成库并运行
all : lib_a link_a run

# 测试
debug :
	@rm -r $(LIB_PATH)/*

# 清除
clean:
	@rm -r $(OBJ_PATH)/* $(LIB_PATH)/*

.PHONY: lib_a link_a run all debug clean